package com.tiancheng.trade.payment.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiancheng.trade.payment.bo.SettleFlowDetailMerchantBO;
import com.tiancheng.trade.payment.channel.bo.PaymentFlowDetailBO;
import com.tiancheng.trade.payment.model.PaymentFlowDetail;
import com.tiancheng.trade.payment.vo.api.QueryFlowDetailVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 支付流水明细持久化接口
 *
 * @author kellen
 * @date 2024/4/29
 */
@Repository
public interface PaymentFlowDetailMapper extends TYBaseMapper<PaymentFlowDetail> {

    /**
     * 保存支付流水明细
     *
     * @param flowDetails
     * @return
     */
    int save(List<PaymentFlowDetail> flowDetails);

    /**
     * 查询流水明细
     *
     * @param queryFlowDetailVO
     * @param page
     * @return
     */
    IPage<Map> find(@Param("flowDetail") QueryFlowDetailVO queryFlowDetailVO, Page<?> page);

    /**
     * 聚合应清分商户、商品编码
     *
     * @param expectSettleDate 渠道应核销日期
     * @param channelCode      渠道编码
     * @return
     */
    List<PaymentFlowDetail> distinctSettle(@Param("expectSettleDate") LocalDate expectSettleDate, @Param("channelCode") String channelCode);


    /**
     * 聚合应清分商户信息
     *
     * @param expectSettleDate 渠道应核销日期
     * @param channelCode      渠道编码
     * @return
     */
    List<SettleFlowDetailMerchantBO> distinctSettleMerchant(@Param("expectSettleDate") LocalDate expectSettleDate, @Param("channelCode") String channelCode);


    /**
     * 查询应清分子订单，每次最多查询1000条
     *
     * @param expectSettleDate 渠道应核销日期
     * @param channel          支付渠道
     * @param startId          起始Id
     * @return
     */
    List<PaymentFlowDetailBO> listSettle(@Param("expectSettleDate") LocalDate expectSettleDate, @Param("channel") String channel, @Param("startId") Long startId);

    /**
     * 流水明细批量更新-所更新字段值相同
     *
     * @param ids       id
     * @param colValMap 字段-值：(column, value)
     * @return
     */
    int updateSettleBatch(@Param("ids") List<Long> ids, @Param("colValMap") Map<String, Object> colValMap);

    int updateSettleBatchBySubFlowId(@Param("subFlowIds") List<String> subFlowIds, @Param("colValMap") Map<String, Object> colValMap);

    /**
     * 修改商户企业号信息
     *
     * @param list
     */
    void updateMerchantBatch(List<PaymentFlowDetail> list);


    /**
     * 聚合应清分商户、商品编码
     *
     * @param expectSettleBegin 优惠统计开始日期
     * @param expectSettleEnd   优惠统计开始日期
     * @return
     */
    List<PaymentFlowDetail> queryCouponSettleMerchant(@Param("expectSettleTimeBegin") LocalDateTime expectSettleBegin, @Param("expectSettleTimeEnd") LocalDateTime expectSettleEnd);

    /**
     * 查询需要清分优惠券的流水明细
     *
     * @param expectSettleBegin 统计开始日期
     * @param expectSettleEnd   统计截止日期
     * @param startId           流水开始id
     * @return
     */
    List<PaymentFlowDetailBO> queryCouponSettlePayFlowDetail(@Param("expectSettleTimeBegin") LocalDateTime expectSettleBegin, @Param("expectSettleTimeEnd") LocalDateTime expectSettleEnd, @Param("startId") long startId);

    /**
     * 查询流水明细
     * @param subFlowIds 子流水号
     * @return
     */
    List<PaymentFlowDetailBO> queryBySubFlowIds(@Param("subFlowIds") Set<String> subFlowIds);
}
